package agency.sales;

import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

import agency.Sales;
import common.Invoice;
import dict.AgencyDict;
import dict.BcDict;
import dict.agency.BocDict;
import entities.TBank;
import entities.TBc;
import entities.TCard;
import entities.TTransaction;
import util.HttpUtil;
import util.LogUtil;
import utils.AesUtil;
import utils.CurrencyUtils;
import utils.DateUtil;
import utils.DigestUtil;
import utils.NumberUtil;
import utils.StringUtil;

public class Bocedc implements Sales {

	private String PATH = "https://172.16.10.98/GatewayT/bgsmoto";
	private String RETU = "http://172.20.131.1/ecp/Gateway";

	public void process(TBc bc, TTransaction transaction, TBank bank, TCard card) {
		Map<String, String> requestMap = new TreeMap<String, String>();
		Map<String,String> responseMap = new TreeMap<String,String>(); 
		String res = null;
		String[] params = null;
		String invoice =null;
		// 消费
		
		try {
			invoice = Invoice.fetchInvoice();
		} catch (Exception e1) {
			bc.setBcMark("555");
			bc.setBcInfo("edc sale data invoice exception");
			bc.setBcCode(AgencyDict.BOC_INVOICE_EXCEPTION);
			bc.setBcMessage(AgencyDict.AGENCY.get(AgencyDict.BOC_ENCRYPT_EXCEPTION));
			return;
		}
		
		requestMap.put("Trans_Type", "sales");
		requestMap.put("Trans_Model", "M");
		requestMap.put("Merchant_Id", bank.getBnkMer());
		requestMap.put("Author_Str", bank.getBnkAuth());
		requestMap.put("Terminal_Id", bank.getBnkTer());
		requestMap.put("Invoice_No", invoice);
		requestMap.put("Currency_Code_T", "156");
		requestMap.put("bocs_ReturnURL", RETU);
		requestMap.put("Amount_Loc", CurrencyUtils.yuan2fen(NumberUtil.transfer(bc.getBcAmount())));
		try {
			String number=card.getCrdNumber();
			String expire=AesUtil.AES_Decrypt(AesUtil.KEY, card.getCrdExpire());
			String cvv=AesUtil.AES_Decrypt(AesUtil.KEY, card.getCrdCvv());
			requestMap.put("Card_No", number);
			requestMap.put("Exp_Date", expire);
			requestMap.put("CSC", cvv);
		} catch (Exception e) {
			bc.setBcMark("555");
			bc.setBcInfo("edc sale data encrpt exception");
			bc.setBcCode(AgencyDict.BOC_ENCRYPT_EXCEPTION);
			bc.setBcMessage(AgencyDict.AGENCY.get(AgencyDict.BOC_ENCRYPT_EXCEPTION));
			return;
		}
		requestMap.put("Order_No", bc.getBcNo());
		requestMap.put("Custom", "");
		
		StringBuffer sb = new StringBuffer();
		sb.append(bank.getBnkKey());
		for (Map.Entry<String, String> e : requestMap.entrySet()) {
			sb.append(e.getValue());
		}
		requestMap.put("HASH", DigestUtil.sha256(sb.toString()));
		
		// 日志打印发送数据
		StringBuffer send = new StringBuffer();
		for (Map.Entry<String, String> e : requestMap.entrySet()) {
			send.append(e.getKey() + "=" + e.getValue() + "&");
		}
		LogUtil.agency("BOCEDC_SALES", send.toString());

		try {
			res = new HttpUtil().ssl(PATH, requestMap);
		} catch (Exception e) {
			bc.setBcMark("555");
			bc.setBcInfo("edc sale data encrpt exception");
			bc.setBcCode(AgencyDict.BOC_TRANSMISSION_EXCEPTION);
			bc.setBcMessage(AgencyDict.AGENCY.get(AgencyDict.BOC_TRANSMISSION_EXCEPTION));
			return;
		}

		if (StringUtil.isEmpty(res)) {
			bc.setBcMark("555");
			bc.setBcInfo("edc sale data response exception");
			bc.setBcCode(AgencyDict.BOC_TRANSMISSION_EXCEPTION);
			bc.setBcMessage(AgencyDict.AGENCY.get(AgencyDict.BOC_TRANSMISSION_EXCEPTION));
			return;
		}
		
		try {
			params = res.split("&");
			for (int i = 0; i < params.length; i++) {
				String[] p = params[i].split("=");
				if (p.length == 2) {
					responseMap.put(p[0], p[1]);
				}
			}
		} catch (Exception e) {
			bc.setBcMark("555");
			bc.setBcInfo("edc sale data disembly exception");
			bc.setBcCode(AgencyDict.BOC_TRANSMISSION_EXCEPTION);
			bc.setBcMessage(AgencyDict.AGENCY.get(AgencyDict.BOC_TRANSMISSION_EXCEPTION));
			return;
		}

		// 日志打印返回数据
		StringBuffer reve = new StringBuffer();
		for (Map.Entry<String, String> e : responseMap.entrySet()) {
			reve.append(e.getKey() + "=" + e.getValue() + "&");
		}
		LogUtil.agency("BOCEDC_SALES", reve.toString());

		if (!responseMap.containsKey("Resp_Code")) {
			bc.setBcMark("555");
			bc.setBcInfo("edc sale data parameter exception");
			bc.setBcCode(AgencyDict.BOC_TRANSMISSION_EXCEPTION);
			bc.setBcMessage(AgencyDict.AGENCY.get(AgencyDict.BOC_TRANSMISSION_EXCEPTION));
			return;
		}
		
		if (!responseMap.get("Resp_Code").equals("0000")) {
			bc.setBcMark(responseMap.get("Resp_Code"));
			bc.setBcInfo(BocDict.message(responseMap.get("Resp_Code")));
			bc.setBcCode(AgencyDict.BOC_SALES_FAILURE);
			bc.setBcMessage(AgencyDict.AGENCY.get(AgencyDict.BOC_SALES_FAILURE));
			return;
		}

		bc.setBcState("1");
		bc.setBcRef(responseMap.get("Ref_No").toString());
		bc.setBcCard(card.getCrdFake());
		bc.setBcTime(DateUtil.format(new Date(), DateUtil.DATE));
		bc.setBcMark(responseMap.get("Resp_Code"));
		bc.setBcInfo(BocDict.message(responseMap.get("Resp_Code")));
		bc.setBcCode(BcDict.BC_SUCCESS);
		bc.setBcMessage(BcDict.MESSAGES.get(BcDict.BC_SUCCESS));
		return;
	}

}